package com.lx.rabbitmq;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;

import utils.ConnectionUtils;

public class Receiver1 {
	private static final String EXCHANGE_NAME = "exchange_route";// 交换机名字
	private static final String QUEUE_NAME = "queue_route"; // 队列名

	public static void main(String[] args) throws Exception {
		Connection conn = ConnectionUtils.getMQConnection();
		final Channel channel = conn.createChannel();

		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
		// 声明交换机
		channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
		// 把队列和交换机以及路右键绑定起来
		channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "error");
		channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "info");

		Consumer consumer = new DefaultConsumer(channel) {
			public void handleDelivery(String consumerTag, com.rabbitmq.client.Envelope envelope,
					BasicProperties properties, byte[] body) throws java.io.IOException {
				try {
					String msg = new String(body);
					System.out.println("Receiver[1]:" + msg);
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					channel.basicAck(envelope.getDeliveryTag(), false);
				}
			};
		};
		channel.basicConsume(QUEUE_NAME, false, consumer);
	}
}
